<?php

namespace app\xcxapi\service;

use app\common\service\BaseService;
use app\models\ClassStudentWorkModel;
use app\models\CommentModel;
use app\models\ContractModel;
use app\models\CourseContractModel;
use app\models\CourseModel;
use app\models\DictionaryModel;
use app\models\FollowUpLogModel;
use app\models\GoodsContractModel;
use app\models\GradesContractModel;
use app\models\GradesDetailsListsModel;
use app\models\GradesDetailsModel;
use app\models\MyComplaintSuggestionModel;
use app\models\MyNoticeModel;
use app\models\MyNoticeObjectModel;
use app\models\ParentInformationModel;
use app\models\RefundAmountModel;
use app\models\StudentModel;
use app\models\StudentSubjectClassModel;
use app\models\StudentSubjectInfosClassModel;
use app\models\WorkAnalysisModel;
use app\models\WorkModel;
use think\Db;

class OverviewService extends BaseService
{
    /**
     * 招生总览
     * User: wufeng
     * Date: 2020/02/21.
     *
     * @param $params
     *
     * @return array
     */
    public function recruitStudentsOverview($params)
    {
        try {
            if (1 === (int) $params['api_type']) {
                $this->jinqi();
            } elseif (2 === (int) $params['api_type']) {
                $this->zonglan();
            }
        } catch (\Exception $e) {
            $this->errorMessage($e, $params);
        }

        return $this->return;
    }

    /**
     * User: wufeng
     * Date: 2020/03/16.
     *
     * @param $params
     *
     * @return array
     */
    public function recentDetailLists($params)
    {
        try {
            $where = [];
            if (5 === (int) xcxapi_get_current_wtype()) {
                $where['user_id'] = xcxapi_get_current_user_id();
            } else {
                $where['campus_id'] = ['in', xcxapi_get_current_campus_id()];
            }
            if (1 === (int) $params['api_type']) {
                //1 新增客户
                $this->data = (new StudentModel())
                    ->where($where)
                    ->whereTime('register_date', 'week')
					->order('id desc')
                    ->select()
                    ->toArray();
            } elseif (2 === (int) $params['api_type']) {
                //2 未分配
                $where['follow_up'] = 33;
                $this->data = (new StudentModel())
                    ->where($where)
                    ->whereTime('register_date', 'week')
					->order('id desc')
                    ->select()
                    ->toArray();
            } elseif (3 === (int) $params['api_type']) {
                //3 未处理
                $where['follow_up'] = 34;
                $this->data = (new StudentModel())
                    ->where($where)
                    ->whereTime('register_date', 'week')
					->order('id desc')
                    ->select()
                    ->toArray();
            } elseif (4 === (int) $params['api_type']) {
                //4 已跟进
                $where['follow_up'] = 35;
                $this->data = (new StudentModel())
                    ->where($where)
                    ->whereTime('register_date', 'week')
					->order('id desc')
                    ->select()
                    ->toArray();
            } elseif (5 === (int) $params['api_type']) {
                //5 已预约
                $where['follow_up'] = 36;
                $this->data = (new StudentModel())
                    ->where($where)
                    ->whereTime('register_date', 'week')
					->order('id desc')
                    ->select()
                    ->toArray();
            } elseif (6 === (int) $params['api_type']) {
                //6 已上门
                $where['follow_up'] = 103;
                $this->data = (new StudentModel())
                    ->where($where)
                    ->whereTime('register_date', 'week')
					->order('id desc')
                    ->select()
                    ->toArray();
            } elseif (7 === (int) $params['api_type']) {
                //7 已交定金
                $where['follow_up'] = 104;
                $this->data = (new StudentModel())
                    ->where($where)
                    ->whereTime('register_date', 'week')
					->order('id desc')
                    ->select()
                    ->toArray();
            } elseif (8 === (int) $params['api_type']) {
                //8 已丢单
                $where['follow_up'] = 105;
                $this->data = (new StudentModel())
                    ->where($where)
                    ->whereTime('register_date', 'week')
					->order('id desc')
                    ->select()
                    ->toArray();
            } elseif (9 === (int) $params['api_type']) {
                //9 待跟进
                $where['next_follow_up_timestamp'] = ['egt', time()];
                $this->data = (new StudentModel())
                    ->where($where)
                    ->whereTime('register_date', 'week')
					->order('id desc')
                    ->select()
                    ->toArray();
            } elseif (10 === (int) $params['api_type']) {
                //10 待上门
                $where['follow_up'] = 36;
                $where['visiting_timestamp'] = ['egt', time()];
                $this->data = (new StudentModel())
                    ->where($where)
                    ->whereTime('register_date', 'week')
					->order('id desc')
                    ->select()
                    ->toArray();
            } elseif (11 === (int) $params['api_type']) {
                //11 待试听
                $stay_audition_arr = [];
                $stay_audition = [];
                if (5 === (int) xcxapi_get_current_wtype()) {
                    $stay_audition_arr['user_id'] = xcxapi_get_current_user_id();
                } else {
                    $stay_audition['campus_id'] = ['in', xcxapi_get_current_campus_id()];
                }
                $stay_audition['report_status'] = 3;
                $stay_audition['status'] = 0;
                $stay_audition_student_ids = (new StudentSubjectInfosClassModel())->where($stay_audition)->column('student_id');
                $stay_audition_arr['id'] = ['in', implode(',', array_values(array_unique($stay_audition_student_ids)))];
                $this->data = (new StudentModel())
                    ->where($stay_audition_arr)
                    ->whereTime('register_date', 'week')
					->order('id desc')
                    ->select()
                    ->toArray();
            } elseif (12 === (int) $params['api_type']) {
                //12 已试听
                $stay_audition_arr = [];
                $stay_audition = [];
                if (5 === (int) xcxapi_get_current_wtype()) {
                    $stay_audition_arr['user_id'] = xcxapi_get_current_user_id();
                } else {
                    $stay_audition['campus_id'] = ['in', xcxapi_get_current_campus_id()];
                }
                $stay_audition['report_status'] = 3;
                $stay_audition['status'] = 1;
                $stay_audition_student_ids = (new StudentSubjectInfosClassModel())->where($stay_audition)->column('student_id');
                $stay_audition_arr['id'] = ['in', implode(',', array_values(array_unique($stay_audition_student_ids)))];
                $this->data = (new StudentModel())
                    ->where($stay_audition_arr)
                    ->whereTime('register_date', 'week')
					->order('id desc')
                    ->select()
                    ->toArray();
            } elseif (13 === (int) $params['api_type']) {
                //13 过期未跟进
                $where['next_follow_up_timestamp'] = ['lt', time()];
                $this->data = (new StudentModel())
                    ->where($where)
                    ->whereTime('register_date', 'week')
					->order('id desc')
                    ->select()
                    ->toArray();
            }
        } catch (\Exception $e) {
            $this->errorMessage($e, $params);
        }

        return $this->return;
    }

    /**
     * User: wufeng
     * Date: 2020/03/16.
     *
     * @param $params
     *
     * @return array
     */
    public function overviewDetailLists($params)
    {
        try {
            $where = [];
            if (5 === (int) xcxapi_get_current_wtype()) {
                $where['user_id'] = xcxapi_get_current_user_id();
            } else {
                $where['campus_id'] = ['in', xcxapi_get_current_campus_id()];
            }
            if (1 === (int) $params['api_type']) {
                //总客户数
                $this->data = (new StudentModel())
                    ->where($where)
					->order('id desc')
                    ->select()
                    ->toArray();
            } elseif (2 === (int) $params['api_type']) {
                //已分配客户
                $where['user_id'] = ['not in', xcxapi_get_current_user_id()];
                $where['course_consultant_type'] = 1;
                $where['status'] = 1;
                $this->data = (new StudentModel())
                    ->where($where)
					->order('id desc')
                    ->select()
                    ->toArray();
            } elseif (3 === (int) $params['api_type']) {
                //公海待分配
                $where['user_id'] = 0;
                $where['course_consultant_type'] = 2;
                $where['status'] = 1;
                $this->data = (new StudentModel())
                    ->where($where)
					->order('id desc')
                    ->select()
                    ->toArray();
            } elseif (4 === (int) $params['api_type']) {
                //已上门
                $where['follow_up'] = 103;
                $this->data = (new StudentModel())
                    ->where($where)
					->order('id desc')
                    ->select()
                    ->toArray();
            } elseif (5 === (int) $params['api_type']) {
                //试听
                $audition_arr = [];
                $audition = [];
                if (5 === (int) xcxapi_get_current_wtype()) {
                    $audition['user_id'] = xcxapi_get_current_user_id();
                } else {
                    $audition_arr['campus_id'] = ['in', xcxapi_get_current_campus_id()];
                }
                $audition_arr['student_type'] = 3;
                $student_ids = (new StudentSubjectClassModel())->where($audition_arr)->column('student_id');
                $audition['id'] = ['in', implode(',', array_values(array_unique($student_ids)))];
                $this->data = (new StudentModel())
                    ->where($audition)
					->order('id desc')
                    ->select()
                    ->toArray();
            } elseif (6 === (int) $params['api_type']) {
                //过期未跟进
                $where['next_follow_up_timestamp'] = ['lt', time()];
                $this->data = (new StudentModel())
                    ->where($where)
					->order('id desc')
                    ->select()
                    ->toArray();
            } elseif (7 === (int) $params['api_type']) {
                //跟进记录
                $this->data = (new FollowUpLogModel())
                    ->where($where)
					->order('id desc')
                    ->select()
                    ->toArray();
                foreach ($this->data as &$datum) {
                    $student = (new StudentModel())->get($datum['student_id']);
                    $datum['student_number'] = $student['student_number'];
                    $datum['phone'] = $student['phone'];
                    $datum['student_name'] = $student['student_name'];
                    $datum['follow_up'] = $student['follow_up'];
                    $datum['follow_up_text'] = (new DictionaryModel())->get($student['follow_up'])['name'];
                }
            }
        } catch (\Exception $e) {
            $this->errorMessage($e, $params);
        }

        return $this->return;
    }

    /**
     * User: wufeng
     * Date: 2020/03/16.
     *
     * @param $params
     *
     * @return array
     */
    public function sourceFollowUpIntentionLevel($params)
    {
        try {
            $where = [];
            if (5 === (int) xcxapi_get_current_wtype()) {
                $where['user_id'] = xcxapi_get_current_user_id();
            } else {
                $where['campus_id'] = ['in', xcxapi_get_current_campus_id()];
            }
            if (1 === (int) $params['api_type']) {
                //来源=》转介绍
                $this->data['introduction'] = (new StudentModel())
                    ->where($where)
                    ->where(['introduction' => ['gt', 0]])
                    ->count();
                //来源=》短信
                $this->data['short_message'] = (new StudentModel())
                    ->where($where)
                    ->where(['channel' => 16])
                    ->count();
                //来源=》广告
                $this->data['advertisement'] = (new StudentModel())
                    ->where($where)
                    ->where(['channel' => 18])
                    ->count();
                //来源=》传单
                $this->data['leaflet'] = (new StudentModel())
                    ->where($where)
                    ->where(['channel' => 19])
                    ->count();
                //来源=》网络
                $this->data['network'] = (new StudentModel())
                    ->where($where)
                    ->where(['channel' => 20])
                    ->count();
                //来源=》外呼电话
                $this->data['outside_call'] = (new StudentModel())
                    ->where($where)
                    ->where(['channel' => 23])
                    ->count();
                //来源=》地推
                $this->data['ground_push'] = (new StudentModel())
                    ->where($where)
                    ->where(['channel' => 24])
                    ->count();
                //来源=》其他
                $this->data['other'] = (new StudentModel())
                    ->where($where)
                    ->where(['channel' => 25])
                    ->count();
            } elseif (2 === (int) $params['api_type']) {
                //跟进状态
                //未分配
                $this->data['unallocated'] = (new StudentModel())
                    ->where($where)
                    ->where(['follow_up' => 33])
                    ->count();
                //未处理
                $this->data['untreated'] = (new StudentModel())
                    ->where($where)
                    ->where(['follow_up' => 34])
                    ->count();
                //已跟进
                $this->data['already_follow_up'] = (new StudentModel())
                    ->where($where)
                    ->where(['follow_up' => 35])
                    ->count();
                //已预约
                $this->data['already_booked'] = (new StudentModel())
                    ->where($where)
                    ->where(['follow_up' => 36])
                    ->count();
                //已上门
                $this->data['already_arrived'] = (new StudentModel())
                    ->where($where)
                    ->where(['follow_up' => 103])
                    ->count();
                //已交定金
                $this->data['already_deposit_paid'] = (new StudentModel())
                    ->where($where)
                    ->where(['follow_up' => 104])
                    ->count();
                //已丢单
                $this->data['already_lost_order'] = (new StudentModel())
                    ->where($where)
                    ->where(['follow_up' => 105])
                    ->count();
            } elseif (3 === (int) $params['api_type']) {
                //意向级别
                $this->data['intention_level_1'] = (new StudentModel())
                    ->where($where)
                    ->where(['intentional_grade' => 1])
                    ->count();
                $this->data['intention_level_2'] = (new StudentModel())
                    ->where($where)
                    ->where(['intentional_grade' => 2])
                    ->count();
                $this->data['intention_level_3'] = (new StudentModel())
                    ->where($where)
                    ->where(['intentional_grade' => 3])
                    ->count();
                $this->data['intention_level_4'] = (new StudentModel())
                    ->where($where)
                    ->where(['intentional_grade' => 4])
                    ->count();
                $this->data['intention_level_5'] = (new StudentModel())
                    ->where($where)
                    ->where(['intentional_grade' => 5])
                    ->count();
                $this->data['intention_level_0'] = (new StudentModel())
                    ->where($where)
                    ->where(['intentional_grade' => 0])
                    ->count();
            }
        } catch (\Exception $e) {
            $this->errorMessage($e, $params);
        }

        return $this->return;
    }

    /**
     * User: wufeng
     * Date: 2020/03/16.
     *
     * @param $params
     *
     * @return array
     */
    public function sourceLists($params)
    {
        try {
            $where = [];
            if (5 === (int) xcxapi_get_current_wtype()) {
                $where['user_id'] = xcxapi_get_current_user_id();
            } else {
                $where['campus_id'] = ['in', xcxapi_get_current_campus_id()];
            }
            if (1 === (int) $params['api_type']) {
                //来源=》转介绍
                $where['introduction'] = ['gt', 0];
            } elseif (2 === (int) $params['api_type']) {
                //来源=》短信
                $where['channel'] = 16;
            } elseif (3 === (int) $params['api_type']) {
                //来源=》广告
                $where['channel'] = 18;
            } elseif (4 === (int) $params['api_type']) {
                //来源=》传单
                $where['channel'] = 19;
            } elseif (5 === (int) $params['api_type']) {
                //来源=》网络
                $where['channel'] = 20;
            } elseif (6 === (int) $params['api_type']) {
                //来源=》外呼电话
                $where['channel'] = 23;
            } elseif (7 === (int) $params['api_type']) {
                //来源=》地推
                $where['channel'] = 24;
            } elseif (8 === (int) $params['api_type']) {
                //来源=》其他
                $where['channel'] = 25;
            }
            $this->data = (new StudentModel())
                ->where($where)
				->order('id desc')
                ->select()
                ->toArray();
        } catch (\Exception $e) {
            $this->errorMessage($e, $params);
        }

        return $this->return;
    }

    /**
     * User: wufeng
     * Date: 2020/03/16.
     *
     * @param $params
     *
     * @return array
     */
    public function followUpStatusLists($params)
    {
        try {
            $where = [];
            if (5 === (int) xcxapi_get_current_wtype()) {
                $where['user_id'] = xcxapi_get_current_user_id();
            } else {
                $where['campus_id'] = ['in', xcxapi_get_current_campus_id()];
            }
            //跟进状态
            if (1 === (int) $params['api_type']) {
                //未分配
                $where['follow_up'] = 33;
            } elseif (2 === (int) $params['api_type']) {
                //未处理
                $where['follow_up'] = 34;
            } elseif (3 === (int) $params['api_type']) {
                //已跟进
                $where['follow_up'] = 35;
            } elseif (4 === (int) $params['api_type']) {
                //已预约
                $where['follow_up'] = 36;
            } elseif (5 === (int) $params['api_type']) {
                //已上门
                $where['follow_up'] = 103;
            } elseif (6 === (int) $params['api_type']) {
                //已交定金
                $where['follow_up'] = 104;
            } elseif (7 === (int) $params['api_type']) {
                //已丢单
                $where['follow_up'] = 105;
            }
            $this->data = (new StudentModel())
                ->where($where)
				->order('id desc')
                ->select()
                ->toArray();
        } catch (\Exception $e) {
            $this->errorMessage($e, $params);
        }

        return $this->return;
    }

    /**
     * User: wufeng
     * Date: 2020/03/16.
     *
     * @param $params
     *
     * @return array
     */
    public function intentionLevelLists($params)
    {
        try {
            $where = [];
            if (5 === (int) xcxapi_get_current_wtype()) {
                $where['user_id'] = xcxapi_get_current_user_id();
            } else {
                $where['campus_id'] = ['in', xcxapi_get_current_campus_id()];
            }
            $where['intentional_grade'] = $params['api_type'];
            $this->data = (new StudentModel())
                ->where($where)
				->order('id desc')
                ->select()
                ->toArray();
        } catch (\Exception $e) {
            $this->errorMessage($e, $params);
        }

        return $this->return;
    }

    /**
     * 收费总览
     * User: wufeng
     * Date: 2020/02/21.
     *
     * @param $params
     *
     * @return array
     */
    public function chargeOverview($params)
    {
        try {
            if (1 === (int) $params['api_type']) {
                //近期
                $this->recentCharge();
            } elseif (2 === (int) $params['api_type']) {
                //总览
                $this->overviewCharge();
            }
        } catch (\Exception $e) {
            $this->errorMessage($e, $params);
        }

        return $this->return;
    }

    /**
     * 课消总览
     * User: wufeng
     * Date: 2020/02/21.
     *
     * @param $params
     *
     * @return array
     */
    public function classEliminationOverview($params)
    {
        try {
            if (1 === (int) $params['api_type']) {
                //近期
                $this->recentClassElimination();
            } elseif (2 === (int) $params['api_type']) {
                //总览
                $this->overviewClassElimination();
            }
        } catch (\Exception $e) {
            $this->errorMessage($e, $params);
        }

        return $this->return;
    }

    /**
     * User: wufeng
     * Date: 2020/03/18.
     *
     * @param $params
     *
     * @return array
     */
    public function classEliminationOverviewDetail($params)
    {
        try {
            $where = $_where =[];
            if (5 === (int) xcxapi_get_current_wtype()) {
				if(2 === (int) xcxapi_get_current_group_type()){
					//2:老师类
					$_where['user_teacher_id'] = xcxapi_get_current_user_id();
				}elseif (3 === (int) xcxapi_get_current_group_type()){
					//3:助教类
					$_where['user_assistant_id'] = xcxapi_get_current_user_id();
				}elseif (4 === (int) xcxapi_get_current_group_type()){
					//4:班主任类
					$_where['user_master_id'] = xcxapi_get_current_user_id();
				}elseif (5 === (int) xcxapi_get_current_group_type()){
					//5:教务类
					$_where['user_edu_id'] = xcxapi_get_current_user_id();
				}
				$gradesDetails_ids = (new GradesDetailsModel())->where($_where)->column('id');
                $where['class_subject_id'] = ['in', implode(',', $gradesDetails_ids)];
            } else {
                $where['campus_id'] = ['in', xcxapi_get_current_campus_id()];
            }
            $where['status'] = 1;
            $where['charging'] = 1;
            if (1 === (int) $params['api_type']) {
                //总课消
            } elseif (2 === (int) $params['api_type']) {
                //欠费课消
                $where['arrears_status'] = 1;
            } elseif (3 === (int) $params['api_type']) {
                //1V1课消
                $course_ids = (new CourseModel())->where(['course_type' => 41])->column('id');
                $where['course_id'] = ['in', implode(',', $course_ids)];
            } elseif (4 === (int) $params['api_type']) {
                //1V1欠费课消
                $course_ids = (new CourseModel())->where(['course_type' => 41])->column('id');
                $where['course_id'] = ['in', implode(',', $course_ids)];
                $where['arrears_status'] = 1;
            } elseif (5 === (int) $params['api_type']) {
                //班课课消
                $course_ids = (new CourseModel())->where(['course_type' => 42])->column('id');
                $where['course_id'] = ['in', implode(',', $course_ids)];
            } elseif (6 === (int) $params['api_type']) {
                //班课欠费课消
                $course_ids = (new CourseModel())->where(['course_type' => 42])->column('id');
                $where['course_id'] = ['in', implode(',', $course_ids)];
                $where['arrears_status'] = 1;
            } elseif (7 === (int) $params['api_type']) {
                //1VN课消
                $course_ids = (new CourseModel())->where(['course_type' => 43])->column('id');
                $where['course_id'] = ['in', implode(',', $course_ids)];
            } elseif (8 === (int) $params['api_type']) {
                //1VN欠费课消
                $course_ids = (new CourseModel())->where(['course_type' => 43])->column('id');
                $where['course_id'] = ['in', implode(',', $course_ids)];
                $where['arrears_status'] = 1;
            }
            if (1 === (int) $params['week']) {
                $this->data = (new StudentSubjectInfosClassModel())
                    ->where($where)
                    ->whereTime('class_date', 'week')
					->order('id desc')
                    ->select()
                    ->toArray();
            } elseif (0 === (int) $params['week']) {
                $this->data = (new StudentSubjectInfosClassModel())
                    ->where($where)
					->order('id desc')
                    ->select()
                    ->toArray();
            }
            foreach ($this->data as &$datum) {
                $gradesDetails = (new GradesDetailsModel())->get($datum['class_subject_id']);
                $gradesDetailsLists = (new GradesDetailsListsModel())->get($datum['class_manage_details_id']);
                $datum['headmaster'] = $gradesDetails['headmaster'];
                $datum['teacher'] = $gradesDetails['teacher'];
                $datum['assistant'] = $gradesDetails['assistant'];
                $datum['grades_name'] = $gradesDetails['grades_name'];
                $datum['class_date'] = $gradesDetailsLists['class_date'];
                $datum['class_subject_start'] = $gradesDetailsLists['class_subject_start'];
                $datum['class_subject_end'] = $gradesDetailsLists['class_subject_end'];
                if (0 === (int) $datum['eliminated_nums']) {
                    $datum['class_times'] = 0;
                } else {
                    $datum['class_times'] = ($datum['amount_deducted'] / $datum['eliminated_nums']) / 100;
                }
            }
        } catch (\Exception $e) {
            $this->errorMessage($e, $params);
        }

        return $this->return;
    }

    /**
     * 服务总览
     * User: wufeng
     * Date: 2020/02/21.
     *
     * @param $params
     *
     * @return array
     */
    public function serviceOverview($params)
    {
        try {
            if (1 === (int) $params['api_type']) {
                $this->recent();
            } elseif (2 === (int) $params['api_type']) {
                $this->overview();
            }
        } catch (\Exception $e) {
            $this->errorMessage($e, $params);
        }

        return $this->return;
    }

    /**
     * User: wufeng
     * Date: 2020/03/17.
     *
     * @param $params
     *
     * @return array
     */
    public function visitLists($params)
    {
        try {
            $where = $return_visit = $no_return_visit = [];
            if (5 === (int) xcxapi_get_current_wtype()) {
                $where['user_id'] = xcxapi_get_current_user_id();
            } else {
                $where['campus_id'] = ['in', xcxapi_get_current_campus_id()];
            }
            if (1 === (int) $params['api_type']) {
                /// 回访
                $return_visit = (new StudentModel())
                    ->where($where)
                    ->where(['next_follow_up_timestamp' => ['egt', time()]])
                    ->whereTime('register_date', 'week')
					->order('id desc')
                    ->select()
                    ->toArray();
                foreach ($return_visit as &$item) {
                    $item['visit_status'] = 1;
                }
                /// 过期未回访
                $no_return_visit = (new StudentModel())
                    ->where($where)
                    ->where(['next_follow_up_timestamp' => ['lt', time()]])
                    ->whereTime('register_date', 'week')
					->order('id desc')
                    ->select()
                    ->toArray();
                foreach ($no_return_visit as &$item) {
                    $item['visit_status'] = 0;
                }
                $this->data = array_merge($return_visit, $no_return_visit);
            } elseif (2 === (int) $params['api_type']) {
                /// 回访
                $return_visit = (new StudentModel())
                    ->where($where)
                    ->where(['next_follow_up_timestamp' => ['egt', time()]])
					->order('id desc')
                    ->select()
                    ->toArray();
                foreach ($return_visit as &$item) {
                    $item['visit_status'] = 1;
                }
                /// 过期未回访
                $no_return_visit = (new StudentModel())
                    ->where($where)
                    ->where(['next_follow_up_timestamp' => ['lt', time()]])
					->order('id desc')
                    ->select()
                    ->toArray();
                foreach ($no_return_visit as &$item) {
                    $item['visit_status'] = 0;
                }
            }
            $this->data = array_merge($return_visit, $no_return_visit);
        } catch (\Exception $e) {
            $this->errorMessage($e, $params);
        }

        return $this->return;
    }

    /**
     * User: wufeng
     * Date: 2020/03/17.
     *
     * @param $params
     *
     * @return array
     */
    public function complaintSuggestionLists($params)
    {
        try {
            if (1 === (int) $params['api_type']) {
                $this->data = (new MyComplaintSuggestionModel())
                    ->whereTime('create_time', 'week')
                    ->order('id desc')
                    ->select()
                    ->toArray();
            } elseif (2 === (int) $params['api_type']) {
                $this->data = (new MyComplaintSuggestionModel())
                    ->order('id desc')
                    ->select()
                    ->toArray();
            }
            foreach ($this->data as &$datum) {
                $datum['picture'] = explode(',', $datum['picture']) ? explode(',', $datum['picture']) : [];
                $parentInformation = (new ParentInformationModel())->get($datum['user_id']);
                $datum['name'] = $parentInformation['name'];
                $datum['student_name'] = (new StudentModel())->get($parentInformation['student_id'])['student_name'];
            }
        } catch (\Exception $e) {
            $this->errorMessage($e, $params);
        }

        return $this->return;
    }

    protected function recentCharge()
    {
        $where = $new_sign = $renew = $extension_division = $stored_value = $refund_amount = $renewal_charge = [];
        if (5 === (int) xcxapi_get_current_wtype()) {
            $where['contractor'] = xcxapi_get_current_user_id();
            $stored_value['user_id'] = xcxapi_get_current_user_id();
            $refund_amount['experienced_person'] = xcxapi_get_current_user_id();
            $contract_ids = (new ContractModel())
                ->where(['contractor' => xcxapi_get_current_user_id()])
                ->whereTime('signing_date', 'week')
                ->column('id');
            $new_sign['contract_id'] = ['in', implode(',', $contract_ids)];
            $renew['contract_id'] = ['in', implode(',', $contract_ids)];
            $extension_division['contract_id'] = ['in', implode(',', $contract_ids)];

            $gradesDetails_ids = (new GradesDetailsModel())->where(['user_teacher_id' => xcxapi_get_current_user_id()])->column('id');
            $renewal_charge['class_subject_id'] = ['in', implode(',', $gradesDetails_ids)];
        } else {
            $where['campus_id'] = ['in', xcxapi_get_current_campus_id()];
            $new_sign['campus_id'] = ['in', xcxapi_get_current_campus_id()];
            $renew['campus_id'] = ['in', xcxapi_get_current_campus_id()];
            $extension_division['campus_id'] = ['in', xcxapi_get_current_campus_id()];
            $stored_value['campus_id'] = ['in', xcxapi_get_current_campus_id()];
            $refund_amount['campus_id'] = ['in', xcxapi_get_current_campus_id()];
            $renewal_charge['campus_id'] = ['in', xcxapi_get_current_campus_id()];
        }
        $general_signature_count = (new ContractModel())
            ->where($where)
            ->whereTime('signing_date', 'week')
            ->count();
        $general_signature_total_amount = ((new ContractModel())
            ->where($where)
            ->whereTime('signing_date', 'week')
            ->sum('total_amount') / 100);

        //总签单
        $this->data['general_signature'] = $general_signature_total_amount.'元'.' / '.$general_signature_count.'笔';
        //新签
        $new_sign['contract_type'] = 127;
        $this->data['new_sign'] = $this->signing($new_sign);
        //储值
        $stored_value_count = (new StudentModel())->where($stored_value)->count();
        $stored_value_storedvalue = ((new StudentModel())->where($stored_value)->sum('storedvalue') / 100);
        $this->data['stored_value'] = $stored_value_storedvalue.'元'.' / '.$stored_value_count.'笔';
        //续费
        $renew['contract_type'] = 128;
        $this->data['renew'] = $this->signing($renew);
        //扩科
        $extension_division['contract_type'] = 129;
        $this->data['extension_division'] = $this->signing($extension_division);
        //欠交
        $owe_count = (new ContractModel())
            ->where($where)
            ->where(['stay_amount' => ['gt', 0]])
            ->whereTime('signing_date', 'week')
            ->count();
        $owe_total_stay_amount = ((new ContractModel())
            ->where($where)
            ->where(['stay_amount' => ['gt', 0]])
            ->whereTime('signing_date', 'week')
            ->sum('stay_amount') / 100);
        $this->data['owe'] = $owe_total_stay_amount.'元'.' / '.$owe_count.'笔';
        //待续费
		$value = Db::name('edu_system_config')->where(['name'=>'renewal_threshold'])->value('value');
        $renewal_charge['surplus_class_elimination'] = ['lt', $value];
        $student_ids = (new StudentSubjectInfosClassModel())
            ->where($renewal_charge)
            ->whereTime('class_date', 'week')
            ->column('student_id');
        $this->data['renewal_charge'] = \count(array_unique($student_ids));
        //退费
        $refund_amount_count = (new RefundAmountModel())
            ->where($refund_amount)
            ->whereTime('create_time', 'week')
            ->count();
        $refund_amount_sum = ((new RefundAmountModel())
            ->where($refund_amount)
            ->whereTime('create_time', 'week')
            ->sum('refund_amount') / 100);
        $this->data['refund_amount'] = $refund_amount_sum.'元'.' / '.$refund_amount_count.'笔';
    }

    protected function overviewCharge()
    {
        $where = $new_sign = $renew = $extension_division = $stored_value = $refund_amount = [];
        if (5 === (int) xcxapi_get_current_wtype()) {
            $where['contractor'] = xcxapi_get_current_user_id();
            $stored_value['user_id'] = xcxapi_get_current_user_id();
            $refund_amount['experienced_person'] = xcxapi_get_current_user_id();
            $contract_ids = (new ContractModel())
                ->where(['contractor' => xcxapi_get_current_user_id()])
                ->column('id');
            $new_sign['contract_id'] = ['in', implode(',', $contract_ids)];
            $renew['contract_id'] = ['in', implode(',', $contract_ids)];
            $extension_division['contract_id'] = ['in', implode(',', $contract_ids)];
        } else {
            $where['campus_id'] = ['in', xcxapi_get_current_campus_id()];
            $new_sign['campus_id'] = ['in', xcxapi_get_current_campus_id()];
            $renew['campus_id'] = ['in', xcxapi_get_current_campus_id()];
            $extension_division['campus_id'] = ['in', xcxapi_get_current_campus_id()];
            $stored_value['campus_id'] = ['in', xcxapi_get_current_campus_id()];
            $refund_amount['campus_id'] = ['in', xcxapi_get_current_campus_id()];
        }
        $general_signature_count = (new ContractModel())
            ->where($where)
            ->count();
        $general_signature_total_amount = ((new ContractModel())
            ->where($where)
            ->sum('total_amount') / 100);

        //总签单
        $this->data['general_signature'] = $general_signature_total_amount.'元'.' / '.$general_signature_count.'笔';
        //新签
        $new_sign['contract_type'] = 127;
        $this->data['new_sign'] = $this->signingOverview($new_sign);
        //储值
        $stored_value_count = (new StudentModel())->where($stored_value)->count();
        $stored_value_storedvalue = ((new StudentModel())->where($stored_value)->sum('storedvalue') / 100);
        $this->data['stored_value'] = $stored_value_storedvalue.'元'.' / '.$stored_value_count.'笔';
        //续费
        $renew['contract_type'] = 128;
        $this->data['renew'] = $this->signingOverview($renew);
        //扩科
        $extension_division['contract_type'] = 129;
        $this->data['extension_division'] = $this->signingOverview($extension_division);
        //欠交
        $owe_count = (new ContractModel())
            ->where($where)
            ->where(['stay_amount' => ['gt', 0]])
            ->count();
        $owe_total_stay_amount = ((new ContractModel())
            ->where($where)
            ->where(['stay_amount' => ['gt', 0]])
            ->sum('stay_amount') / 100);
        $this->data['owe'] = $owe_total_stay_amount.'元'.' / '.$owe_count.'笔';
        //待续费
		$value = Db::name('edu_system_config')->where(['name'=>'renewal_threshold'])->value('value');
        $renewal_charge['surplus_class_elimination'] = ['lt', $value];
        $student_ids = (new StudentSubjectInfosClassModel())
            ->where($renewal_charge)
            ->whereTime('class_date', 'week')
            ->column('student_id');
        $this->data['renewal_charge'] = \count(array_unique($student_ids));
        //退费
        $refund_amount_count = (new RefundAmountModel())
            ->where($refund_amount)
            ->whereTime('create_time', 'week')
            ->count();
        $refund_amount_sum = ((new RefundAmountModel())
            ->where($refund_amount)
            ->whereTime('create_time', 'week')
            ->sum('refund_amount') / 100);
        $this->data['refund_amount'] = $refund_amount_sum.'元'.' / '.$refund_amount_count.'笔';
    }

    protected function signing($signing)
    {
        if (5 === (int) xcxapi_get_current_wtype()) {
            $course_contract_sign_up_count = (new CourseContractModel())->where($signing)->count();
            $goods_contract_sign_up_count = (new GoodsContractModel())->where($signing)->count();
            $grades_contract_sign_up_count = (new GradesContractModel())->where($signing)->count();
            $contract_count = $course_contract_sign_up_count + $goods_contract_sign_up_count + $grades_contract_sign_up_count;
            $course_contract_sign_up_net_receipts = ((new CourseContractModel())->where($signing)->sum('net_receipts') / 100);
            $goods_contract_sign_up_net_receipts = ((new GoodsContractModel())->where($signing)->sum('net_receipts') / 100);
            $grades_contract_sign_up_net_receipts = ((new GradesContractModel())->where($signing)->sum('net_receipts') / 100);
            $contract_net_receipts = $course_contract_sign_up_net_receipts + $goods_contract_sign_up_net_receipts + $grades_contract_sign_up_net_receipts;
        } else {
            $course_contract_sign_up_count = (new CourseContractModel())->where($signing)->whereTime('create_time', 'week')->count();
            $goods_contract_sign_up_count = (new GoodsContractModel())->where($signing)->whereTime('create_time', 'week')->count();
            $grades_contract_sign_up_count = (new GradesContractModel())->where($signing)->whereTime('create_time', 'week')->count();
            $contract_count = $course_contract_sign_up_count + $goods_contract_sign_up_count + $grades_contract_sign_up_count;
            $course_contract_sign_up_net_receipts = ((new CourseContractModel())->where($signing)->whereTime('create_time', 'week')->sum('net_receipts') / 100);
            $goods_contract_sign_up_net_receipts = ((new GoodsContractModel())->where($signing)->whereTime('create_time', 'week')->sum('net_receipts') / 100);
            $grades_contract_sign_up_net_receipts = ((new GradesContractModel())->where($signing)->whereTime('create_time', 'week')->sum('net_receipts') / 100);
            $contract_net_receipts = $course_contract_sign_up_net_receipts + $goods_contract_sign_up_net_receipts + $grades_contract_sign_up_net_receipts;
        }

        return $contract_net_receipts.'元'.' / '.$contract_count.'笔';
    }

    protected function signingOverview($signing)
    {
        $course_contract_sign_up_count = (new CourseContractModel())->where($signing)->count();
        $goods_contract_sign_up_count = (new GoodsContractModel())->where($signing)->count();
        $grades_contract_sign_up_count = (new GradesContractModel())->where($signing)->count();
        $contract_count = $course_contract_sign_up_count + $goods_contract_sign_up_count + $grades_contract_sign_up_count;
        $course_contract_sign_up_net_receipts = ((new CourseContractModel())->where($signing)->sum('net_receipts') / 100);
        $goods_contract_sign_up_net_receipts = ((new GoodsContractModel())->where($signing)->sum('net_receipts') / 100);
        $grades_contract_sign_up_net_receipts = ((new GradesContractModel())->where($signing)->sum('net_receipts') / 100);
        $contract_net_receipts = $course_contract_sign_up_net_receipts + $goods_contract_sign_up_net_receipts + $grades_contract_sign_up_net_receipts;

        return $contract_net_receipts.'元'.' / '.$contract_count.'笔';
    }

    protected function recentClassElimination()
    {
        $where = $_where = [];
        if (5 === (int) xcxapi_get_current_wtype()) {
        	if(2 === (int) xcxapi_get_current_group_type()){
				//2:老师类
				$_where['user_teacher_id'] = xcxapi_get_current_user_id();
			}elseif (3 === (int) xcxapi_get_current_group_type()){
				//3:助教类
				$_where['user_assistant_id'] = xcxapi_get_current_user_id();
			}elseif (4 === (int) xcxapi_get_current_group_type()){
				//4:班主任类
				$_where['user_master_id'] = xcxapi_get_current_user_id();
			}elseif (5 === (int) xcxapi_get_current_group_type()){
				//5:教务类
				$_where['user_edu_id'] = xcxapi_get_current_user_id();
			}
			$gradesDetails_ids = (new GradesDetailsModel())->where($_where)->column('id');
            $where['class_subject_id'] = ['in', implode(',', $gradesDetails_ids)];
        } else {
            $where['campus_id'] = ['in', xcxapi_get_current_campus_id()];
        }
        $where['status'] = 1;
        $where['charging'] = 1;
        //总课消 总金额
        $amount_deducted = (new StudentSubjectInfosClassModel())
            ->where($where)
            ->whereTime('class_date', 'week')
            ->sum('amount_deducted');
        $total_sum_1 = $amount_deducted / 100;
        //总课消 总人数
        $student_ids_1 = (new StudentSubjectInfosClassModel())
            ->where($where)
            ->whereTime('class_date', 'week')
            ->column('student_id');
        $total_number_1 = \count($student_ids_1);
        $this->data['amount_deducted'] = $total_sum_1.'元'.' / '.$total_number_1.'人次';

        //欠费课消
        $arrears_amount = (new StudentSubjectInfosClassModel())
            ->where($where)
            ->where(['arrears_status' => 1])
            ->whereTime('class_date', 'week')
            ->sum('arrears_amount');
        $total_sum_2 = $arrears_amount / 100;
        $student_ids_2 = (new StudentSubjectInfosClassModel())
            ->where($where)
            ->where(['arrears_status' => 1])
            ->whereTime('class_date', 'week')
            ->column('student_id');
        $total_number_2 = \count($student_ids_2);
        $this->data['arrears_amount'] = $total_sum_2.'元'.' / '.$total_number_2.'人次';

        //1V1课消
        $course_ids = (new CourseModel())->where(['course_type' => 41])->column('id');
        $amount_deducted_1v1 = (new StudentSubjectInfosClassModel())
            ->where($where)
            ->where(['course_id' => ['in', implode(',', $course_ids)]])
            ->whereTime('class_date', 'week')
            ->sum('amount_deducted');
        $total_sum_1v1 = $amount_deducted_1v1 / 100;
        $student_ids_1v1 = (new StudentSubjectInfosClassModel())
            ->where($where)
			->where(['course_id' => ['in', implode(',', $course_ids)]])
            ->whereTime('class_date', 'week')
            ->column('student_id');
        $total_number_1v1 = \count($student_ids_1v1);
        $this->data['amount_deducted_1v1'] = $total_sum_1v1.'元'.' / '.$total_number_1v1.'人次';

        //1V1欠费课消
        $course_ids = (new CourseModel())->where(['course_type' => 41])->column('id');
        $arrears_amount_1v1 = (new StudentSubjectInfosClassModel())
            ->where($where)
            ->where(['arrears_status' => 1])
            ->where(['course_id' => ['in', implode(',', $course_ids)]])
            ->whereTime('class_date', 'week')
            ->sum('arrears_amount');
        $total_sum_1v1 = $arrears_amount_1v1 / 100;
        $student_ids_1v1 = (new StudentSubjectInfosClassModel())
            ->where($where)
            ->where(['arrears_status' => 1])
			->where(['course_id' => ['in', implode(',', $course_ids)]])
            ->whereTime('class_date', 'week')
            ->column('student_id');
        $total_number_1v1 = \count($student_ids_1v1);
        $this->data['arrears_amount_1v1'] = $total_sum_1v1.'元'.' / '.$total_number_1v1.'人次';

        //班课课消
        $course_ids = (new CourseModel())->where(['course_type' => 42])->column('id');
        $amount_deducted_BK = (new StudentSubjectInfosClassModel())
            ->where($where)
            ->where(['course_id' => ['in', implode(',', $course_ids)]])
            ->whereTime('class_date', 'week')
            ->sum('amount_deducted');
        $total_sum_BK = $amount_deducted_BK / 100;
        $student_ids_BK = (new StudentSubjectInfosClassModel())
            ->where($where)
			->where(['course_id' => ['in', implode(',', $course_ids)]])
            ->whereTime('class_date', 'week')
            ->column('student_id');
        $total_number_BK = \count($student_ids_BK);
        $this->data['amount_deducted_BK'] = $total_sum_BK.'元'.' / '.$total_number_BK.'人次';

        //班课欠费课消
        $course_ids = (new CourseModel())->where(['course_type' => 42])->column('id');
        $arrears_amount_BK = (new StudentSubjectInfosClassModel())
            ->where($where)
            ->where(['arrears_status' => 1])
            ->where(['course_id' => ['in', implode(',', $course_ids)]])
            ->whereTime('class_date', 'week')
            ->sum('arrears_amount');
        $total_sum_BK = $arrears_amount_BK / 100;
        $student_ids_BK = (new StudentSubjectInfosClassModel())
            ->where($where)
            ->where(['arrears_status' => 1])
			->where(['course_id' => ['in', implode(',', $course_ids)]])
            ->whereTime('class_date', 'week')
            ->column('student_id');
        $total_number_BK = \count($student_ids_BK);
        $this->data['arrears_amount_BK'] = $total_sum_BK.'元'.' / '.$total_number_BK.'人次';

        //1VN课消
        $course_ids = (new CourseModel())->where(['course_type' => 43])->column('id');
        $amount_deducted_1vN = (new StudentSubjectInfosClassModel())
            ->where($where)
            ->where(['course_id' => ['in', implode(',', $course_ids)]])
            ->whereTime('class_date', 'week')
            ->sum('amount_deducted');
        $total_sum_1vN = $amount_deducted_1vN / 100;
        $student_ids_1vN = (new StudentSubjectInfosClassModel())
            ->where($where)
			->where(['course_id' => ['in', implode(',', $course_ids)]])
            ->whereTime('class_date', 'week')
            ->column('student_id');
        $total_number_1vN = \count($student_ids_1vN);
        $this->data['amount_deducted_1vN'] = $total_sum_1vN.'元'.' / '.$total_number_1vN.'人次';

        //1VN欠费课消
        $course_ids = (new CourseModel())->where(['course_type' => 43])->column('id');
        $arrears_amount_1vN = (new StudentSubjectInfosClassModel())
            ->where($where)
            ->where(['arrears_status' => 1])
            ->where(['course_id' => ['in', implode(',', $course_ids)]])
            ->whereTime('class_date', 'week')
            ->sum('arrears_amount');
        $total_sum_1vN = $arrears_amount_1vN / 100;
        $student_ids_1vN = (new StudentSubjectInfosClassModel())
            ->where($where)
            ->where(['arrears_status' => 1])
			->where(['course_id' => ['in', implode(',', $course_ids)]])
            ->whereTime('class_date', 'week')
            ->column('student_id');
        $total_number_1vN = \count($student_ids_1vN);
        $this->data['arrears_amount_1vN'] = $total_sum_1vN.'元'.' / '.$total_number_1vN.'人次';
    }

    protected function overviewClassElimination()
    {
        $where = $_where = [];
        if (5 === (int) xcxapi_get_current_wtype()) {
			if(2 === (int) xcxapi_get_current_group_type()){
				//2:老师类
				$_where['user_teacher_id'] = xcxapi_get_current_user_id();
			}elseif (3 === (int) xcxapi_get_current_group_type()){
				//3:助教类
				$_where['user_assistant_id'] = xcxapi_get_current_user_id();
			}elseif (4 === (int) xcxapi_get_current_group_type()){
				//4:班主任类
				$_where['user_master_id'] = xcxapi_get_current_user_id();
			}elseif (5 === (int) xcxapi_get_current_group_type()){
				//5:教务类
				$_where['user_edu_id'] = xcxapi_get_current_user_id();
			}
			$gradesDetails_ids = (new GradesDetailsModel())->where($_where)->column('id');
            $where['class_subject_id'] = ['in', implode(',', $gradesDetails_ids)];
        } else {
            $where['campus_id'] = ['in', xcxapi_get_current_campus_id()];
        }
        $where['status'] = 1;
        $where['charging'] = 1;
        //总课消 总金额
        $amount_deducted = (new StudentSubjectInfosClassModel())
            ->where($where)
            ->sum('amount_deducted');
        $total_sum_1 = $amount_deducted / 100;
        //总课消 总人数
        $student_ids_1 = (new StudentSubjectInfosClassModel())
            ->where($where)
            ->column('student_id');
        $total_number_1 = \count($student_ids_1);
        $this->data['amount_deducted'] = $total_sum_1.'元'.' / '.$total_number_1.'人次';

        //欠费课消
        $arrears_amount = (new StudentSubjectInfosClassModel())
            ->where($where)
            ->where(['arrears_status' => 1])
            ->sum('arrears_amount');
        $total_sum_2 = $arrears_amount / 100;
        $student_ids_2 = (new StudentSubjectInfosClassModel())
            ->where($where)
            ->where(['arrears_status' => 1])
            ->column('student_id');
        $total_number_2 = \count($student_ids_2);
        $this->data['arrears_amount'] = $total_sum_2.'元'.' / '.$total_number_2.'人次';

        //1V1课消
        $course_ids = (new CourseModel())->where(['course_type' => 41])->column('id');
        $amount_deducted_1v1 = (new StudentSubjectInfosClassModel())
            ->where($where)
            ->where(['course_id' => ['in', implode(',', $course_ids)]])
            ->sum('amount_deducted');
        $total_sum_1v1 = $amount_deducted_1v1 / 100;
        $student_ids_1v1 = (new StudentSubjectInfosClassModel())
            ->where($where)
			->where(['course_id' => ['in', implode(',', $course_ids)]])
            ->column('student_id');
        $total_number_1v1 = \count($student_ids_1v1);
        $this->data['amount_deducted_1v1'] = $total_sum_1v1.'元'.' / '.$total_number_1v1.'人次';

        //1V1欠费课消
        $course_ids = (new CourseModel())->where(['course_type' => 41])->column('id');
        $arrears_amount_1v1 = (new StudentSubjectInfosClassModel())
            ->where($where)
            ->where(['arrears_status' => 1])
            ->where(['course_id' => ['in', implode(',', $course_ids)]])
            ->sum('arrears_amount');
        $total_sum_1v1 = $arrears_amount_1v1 / 100;
        $student_ids_1v1 = (new StudentSubjectInfosClassModel())
            ->where($where)
            ->where(['arrears_status' => 1])
			->where(['course_id' => ['in', implode(',', $course_ids)]])
            ->column('student_id');
        $total_number_1v1 = \count($student_ids_1v1);
        $this->data['arrears_amount_1v1'] = $total_sum_1v1.'元'.' / '.$total_number_1v1.'人次';

        //班课课消
        $course_ids = (new CourseModel())->where(['course_type' => 42])->column('id');
        $amount_deducted_BK = (new StudentSubjectInfosClassModel())
            ->where($where)
            ->where(['course_id' => ['in', implode(',', $course_ids)]])
            ->sum('amount_deducted');
        $total_sum_BK = $amount_deducted_BK / 100;
        $student_ids_BK = (new StudentSubjectInfosClassModel())
            ->where($where)
			->where(['course_id' => ['in', implode(',', $course_ids)]])
            ->column('student_id');
        $total_number_BK = \count($student_ids_BK);
        $this->data['amount_deducted_BK'] = $total_sum_BK.'元'.' / '.$total_number_BK.'人次';

        //班课欠费课消
        $course_ids = (new CourseModel())->where(['course_type' => 42])->column('id');
        $arrears_amount_BK = (new StudentSubjectInfosClassModel())
            ->where($where)
            ->where(['arrears_status' => 1])
            ->where(['course_id' => ['in', implode(',', $course_ids)]])
            ->sum('arrears_amount');
        $total_sum_BK = $arrears_amount_BK / 100;
        $student_ids_BK = (new StudentSubjectInfosClassModel())
            ->where($where)
            ->where(['arrears_status' => 1])
			->where(['course_id' => ['in', implode(',', $course_ids)]])
            ->column('student_id');
        $total_number_BK = \count($student_ids_BK);
        $this->data['arrears_amount_BK'] = $total_sum_BK.'元'.' / '.$total_number_BK.'人次';

        //1VN课消
        $course_ids = (new CourseModel())->where(['course_type' => 43])->column('id');
        $amount_deducted_1vN = (new StudentSubjectInfosClassModel())
            ->where($where)
            ->where(['course_id' => ['in', implode(',', $course_ids)]])
            ->sum('amount_deducted');
        $total_sum_1vN = $amount_deducted_1vN / 100;
        $student_ids_1vN = (new StudentSubjectInfosClassModel())
            ->where($where)
			->where(['course_id' => ['in', implode(',', $course_ids)]])
            ->column('student_id');
        $total_number_1vN = \count($student_ids_1vN);
        $this->data['amount_deducted_1vN'] = $total_sum_1vN.'元'.' / '.$total_number_1vN.'人次';

        //1VN欠费课消
        $course_ids = (new CourseModel())->where(['course_type' => 43])->column('id');
        $arrears_amount_1vN = (new StudentSubjectInfosClassModel())
            ->where($where)
            ->where(['arrears_status' => 1])
            ->where(['course_id' => ['in', implode(',', $course_ids)]])
            ->sum('arrears_amount');
        $total_sum_1vN = $arrears_amount_1vN / 100;
        $student_ids_1vN = (new StudentSubjectInfosClassModel())
            ->where($where)
            ->where(['arrears_status' => 1])
			->where(['course_id' => ['in', implode(',', $course_ids)]])
            ->column('student_id');
        $total_number_1vN = \count($student_ids_1vN);
        $this->data['arrears_amount_1vN'] = $total_sum_1vN.'元'.' / '.$total_number_1vN.'人次';
    }

    protected function recent()
    {
        ///// 作业 START
        $where = [];
        $work = [];
        if (5 === (int) xcxapi_get_current_wtype()) {
            $where['user_id'] = xcxapi_get_current_user_id();
            $work_ids = (new WorkModel())->where(['user_id' => xcxapi_get_current_user_id()])->column('work_id');
            $work['work_id'] = ['in', implode(',', $work_ids)];
        } else {
            $gradesDetailsLists_ids = (new GradesDetailsListsModel())->where(['campus_id' => ['in', xcxapi_get_current_campus_id()]])->column('id');
            $where['cmdl_id'] = ['in', implode(',', $gradesDetailsLists_ids)];
            $work['cmdl_id'] = ['in', implode(',', $gradesDetailsLists_ids)];
        }
        //发布统计
        $this->data['work']['release'] = (new WorkModel())
            ->where($where)
            ->whereTime('work_time', 'week')
            ->count();

        //接收学员人数
        $this->data['work']['student_num'] = (new ClassStudentWorkModel())
            ->where($work)
            ->whereTime('create_time', 'week')
            ->count();

        //解析
        $this->data['work']['analysis'] = (new WorkAnalysisModel())
            ->where($work)
            ->whereTime('analysis_time', 'week')
            ->count();

        //已读
        $this->data['work']['already_read'] = (new ClassStudentWorkModel())
            ->where($work)
            ->where(['is_read' => 1])
            ->whereTime('create_time', 'week')
            ->count();

        //已交
        $this->data['work']['already_submit'] = (new ClassStudentWorkModel())
            ->where($work)
            ->where(['is_submission' => 1])
            ->whereTime('create_time', 'week')
            ->count();

        //已改
        $this->data['work']['already_modify'] = (new ClassStudentWorkModel())
            ->where($work)
            ->where(['is_correction' => 1])
            ->whereTime('create_time', 'week')
            ->count();
        ///// 作业 END

        ///// 点评 START
        $student_comment = [];
        $teacher_comment = [];
        $evaluate = [];
        if (5 === (int) xcxapi_get_current_wtype()) {
            $student_comment['teacher_id'] = xcxapi_get_current_user_id();
            $teacher_comment['teacher_id'] = xcxapi_get_current_user_id();
            $evaluate['teacher_id'] = xcxapi_get_current_user_id();
        } else {
            $gradesDetailsLists_ids = (new GradesDetailsListsModel())->where(['campus_id' => ['in', xcxapi_get_current_campus_id()]])->column('id');
            $student_comment['cmdl_id'] = ['in', implode(',', $gradesDetailsLists_ids)];
            $teacher_comment['cmdl_id'] = ['in', implode(',', $gradesDetailsLists_ids)];
            $evaluate['cmdl_id'] = ['in', implode(',', $gradesDetailsLists_ids)];
        }
        $student_comment['c_type'] = 1;
        $teacher_comment['c_type'] = 2;
        // c_type 2 老师点评学生;是否答案解析 默认为0未点评；1已点评
        $comments = (new CommentModel())
            ->where($student_comment)
            ->whereTime('comment_time', 'week')
            ->count();

        //近期作业学员分数
        $work_student_total_fraction = (new CommentModel())
            ->where($student_comment)
            ->whereTime('comment_time', 'week')
            ->sum('student_scoring');
        //学生评分
        if (0 === (int) $comments) {
            $this->data['comment']['fraction'] = 0;
        } else {
            $this->data['comment']['fraction'] = $work_student_total_fraction / $comments;
        }

        //学生评价数
        $this->data['comment']['student_evaluate_num'] = (new CommentModel())
            ->where($student_comment)
            ->whereTime('comment_time', 'week')
            ->count();
        //老师评价数
        $this->data['comment']['teacher_evaluate_num'] = (new CommentModel())
            ->where($teacher_comment)
            ->whereTime('comment_time', 'week')
            ->count();
        //评价总数
        $evaluate_num = (new CommentModel())
            ->where($evaluate)
            ->whereTime('comment_time', 'week')
            ->count();

        if (0 === (int) $evaluate_num) {
            //学生评价率
            $this->data['comment']['student_evaluate_rate'] = 0;
            //老师评价率
            $this->data['comment']['teacher_evaluate_rate'] = 0;
        } else {
            //学生评价率
            $this->data['comment']['student_evaluate_rate'] = $this->data['comment']['student_evaluate_num'] / $evaluate_num;
            //老师评价率
            $this->data['comment']['teacher_evaluate_rate'] = $this->data['comment']['teacher_evaluate_num'] / $evaluate_num;
        }
        ////回访
        /// 已回访
        $return_visit = [];
        if (5 === (int) xcxapi_get_current_wtype()) {
            $return_visit['user_id'] = xcxapi_get_current_user_id();
        } else {
            $return_visit['campus_id'] = ['in', xcxapi_get_current_campus_id()];
        }
        $this->data['comment']['return_visit'] = (new StudentModel())
            ->where($return_visit)
            ->where(['next_follow_up_timestamp' => ['egt', time()]])
            ->whereTime('register_date', 'week')
            ->count();
        /// 过期未回访
        $this->data['comment']['no_return_visit'] = (new StudentModel())
            ->where($return_visit)
            ->where(['next_follow_up_timestamp' => ['lt', time()]])
            ->whereTime('register_date', 'week')
            ->count();

        ////通知
        ///已发布通知
        $notice_where = [];
        $notice_object_where = [];
        $notice_where['user_id'] = xcxapi_get_current_user_id();
        $notice_ids = (new MyNoticeModel())->where(['user_id' => xcxapi_get_current_user_id()])->column('id');
        $notice_object_where['notice_id'] = ['in', implode(',', $notice_ids)];
        $this->data['comment']['notice'] = (new MyNoticeModel())
            ->where($notice_where)
            ->whereTime('notice_time', 'week')
            ->count();
        ///已接收
        $this->data['comment']['notice_object'] = (new MyNoticeObjectModel())
            ->where($notice_object_where)
            ->whereTime('create_time', 'week')
            ->count();
        ///已读
        $this->data['comment']['already_read'] = (new MyNoticeObjectModel())
            ->where(['is_read' => 1])
            ->where($notice_object_where)
            ->whereTime('create_time', 'week')
            ->count();

        ////建议投诉
        ///总数
        $this->data['comment']['complaint_suggestion'] = (new MyComplaintSuggestionModel())
            ->whereTime('create_time', 'week')
            ->count();
        ///已处理
        $this->data['comment']['already_untreated'] = (new MyComplaintSuggestionModel())
            ->where(['status' => 1])
            ->whereTime('create_time', 'week')
            ->count();
        ///未处理
        $this->data['comment']['untreated'] = (new MyComplaintSuggestionModel())
            ->where(['status' => 0])
            ->whereTime('create_time', 'week')
            ->count();
        ///// 点评 END
    }

    protected function overview()
    {
        // 作业 START
        $where = [];
        $work = [];
        if (5 === (int) xcxapi_get_current_wtype()) {
            $where['user_id'] = xcxapi_get_current_user_id();
            $work_ids = (new WorkModel())->where(['user_id' => xcxapi_get_current_user_id()])->column('work_id');
            $work['work_id'] = ['in', implode(',', $work_ids)];
        } else {
            $gradesDetailsLists_ids = (new GradesDetailsListsModel())->where(['campus_id' => ['in', xcxapi_get_current_campus_id()]])->column('id');
            $where['cmdl_id'] = ['in', implode(',', $gradesDetailsLists_ids)];
            $work['cmdl_id'] = ['in', implode(',', $gradesDetailsLists_ids)];
        }
        //发布统计
        $this->data['work']['release'] = (new WorkModel())
            ->where($where)
            ->count();

        //接收学员人数
        $this->data['work']['student_num'] = (new ClassStudentWorkModel())
            ->where($work)
            ->count();

        //解析
        $this->data['work']['analysis'] = (new WorkAnalysisModel())
            ->where($work)
            ->count();

        //已读
        $this->data['work']['already_read'] = (new ClassStudentWorkModel())
            ->where($work)
            ->where(['is_read' => 1])
            ->count();

        //已交
        $this->data['work']['already_submit'] = (new ClassStudentWorkModel())
            ->where($work)
            ->where(['is_submission' => 1])
            ->count();

        //已改
        $this->data['work']['already_modify'] = (new ClassStudentWorkModel())
            ->where($work)
            ->where(['is_correction' => 1])
            ->count();
        // 作业 END

        // 点评 START
        $student_comment = [];
        $teacher_comment = [];
        $evaluate = [];
        if (5 === (int) xcxapi_get_current_wtype()) {
            $student_comment['teacher_id'] = xcxapi_get_current_user_id();
            $teacher_comment['teacher_id'] = xcxapi_get_current_user_id();
            $evaluate['teacher_id'] = xcxapi_get_current_user_id();
        } else {
            $gradesDetailsLists_ids = (new GradesDetailsListsModel())->where(['campus_id' => ['in', xcxapi_get_current_campus_id()]])->column('id');
            $student_comment['cmdl_id'] = ['in', implode(',', $gradesDetailsLists_ids)];
            $teacher_comment['cmdl_id'] = ['in', implode(',', $gradesDetailsLists_ids)];
            $evaluate['cmdl_id'] = ['in', implode(',', $gradesDetailsLists_ids)];
        }
        $student_comment['c_type'] = 1;
        $teacher_comment['c_type'] = 2;
        // c_type 2 老师点评学生;是否答案解析 默认为0未点评；1已点评
        $comments = (new CommentModel())
            ->where($student_comment)
            ->count();

        //近期作业学员分数
        $work_student_total_fraction = (new CommentModel())
            ->where($student_comment)
            ->sum('student_scoring');
        //学生评分
        $this->data['comment']['fraction'] = $work_student_total_fraction / $comments;

        //学生评价数
        $this->data['comment']['student_evaluate_num'] = (new CommentModel())
            ->where($student_comment)
            ->count();
        //老师评价数
        $this->data['comment']['teacher_evaluate_num'] = (new CommentModel())
            ->where($teacher_comment)
            ->count();
        //评价总数
        $evaluate_num = (new CommentModel())
            ->where($evaluate)
            ->count();

        if (0 === (int) $evaluate_num) {
            //学生评价率
            $this->data['comment']['student_evaluate_rate'] = 0;
            //老师评价率
            $this->data['comment']['teacher_evaluate_rate'] = 0;
        } else {
            //学生评价率
            $this->data['comment']['student_evaluate_rate'] = $this->data['comment']['student_evaluate_num'] / $evaluate_num;
            //老师评价率
            $this->data['comment']['teacher_evaluate_rate'] = $this->data['comment']['teacher_evaluate_num'] / $evaluate_num;
        }
        ////回访
        /// 已回访
        $return_visit = [];
        if (5 === (int) xcxapi_get_current_wtype()) {
            $return_visit['user_id'] = xcxapi_get_current_user_id();
        } else {
            $return_visit['campus_id'] = ['in', xcxapi_get_current_campus_id()];
        }
        $this->data['comment']['return_visit'] = (new StudentModel())
            ->where($return_visit)
            ->where(['next_follow_up_timestamp' => ['egt', time()]])
            ->count();
        /// 过期未回访
        $this->data['comment']['no_return_visit'] = (new StudentModel())
            ->where($return_visit)
            ->where(['next_follow_up_timestamp' => ['lt', time()]])
            ->count();

        ////通知
        ///已发布通知
        $notice_where = [];
        $notice_object_where = [];
        $notice_where['user_id'] = xcxapi_get_current_user_id();
        $notice_ids = (new MyNoticeModel())->where(['user_id' => xcxapi_get_current_user_id()])->column('id');
        $notice_object_where['notice_id'] = ['in', implode(',', $notice_ids)];
        $this->data['comment']['notice'] = (new MyNoticeModel())
            ->where($notice_where)
            ->count();
        ///已接收
        $this->data['comment']['notice_object'] = (new MyNoticeObjectModel())
            ->where($notice_object_where)
            ->count();
        ///已读
        $this->data['comment']['already_read'] = (new MyNoticeObjectModel())
            ->where(['is_read' => 1])
            ->where($notice_object_where)
            ->count();

        ////建议投诉
        ///总数
        $this->data['comment']['complaint_suggestion'] = (new MyComplaintSuggestionModel())
            ->count();
        ///已处理
        $this->data['comment']['already_untreated'] = (new MyComplaintSuggestionModel())
            ->where(['status' => 1])
            ->count();
        ///未处理
        $this->data['comment']['untreated'] = (new MyComplaintSuggestionModel())
            ->where(['status' => 0])
            ->count();
        // 点评 END
    }

    protected function jinqi()
    {
        $where = [];
        if (5 === (int) xcxapi_get_current_wtype()) {
            $where['user_id'] = xcxapi_get_current_user_id();
        } else {
            $where['campus_id'] = ['in', xcxapi_get_current_campus_id()];
        }
        //新增客户
        $this->data['add_customer'] = (new StudentModel())
            ->where($where)
            ->whereTime('register_date', 'week')
            ->count();
        //未分配
        $this->data['unallocated'] = (new StudentModel())
            ->where($where)
            ->where(['follow_up' => 33])
            ->whereTime('register_date', 'week')
            ->count();
        //未处理
        $this->data['untreated'] = (new StudentModel())
            ->where($where)
            ->where(['follow_up' => 34])
            ->whereTime('register_date', 'week')
            ->count();
        //已跟进
        $this->data['already_follow_up'] = (new StudentModel())
            ->where($where)
            ->where(['follow_up' => 35])
            ->whereTime('register_date', 'week')
            ->count();
        //已预约
        $this->data['already_booked'] = (new StudentModel())
            ->where($where)
            ->where(['follow_up' => 36])
            ->whereTime('register_date', 'week')
            ->count();
        //已上门
        $this->data['already_arrived'] = (new StudentModel())
            ->where($where)
            ->where(['follow_up' => 103])
            ->whereTime('register_date', 'week')
            ->count();
        //已交定金
        $this->data['already_deposit_paid'] = (new StudentModel())
            ->where($where)
            ->where(['follow_up' => 104])
            ->whereTime('register_date', 'week')
            ->count();
        //已丢单
        $this->data['already_lost_order'] = (new StudentModel())
            ->where($where)
            ->where(['follow_up' => 105])
            ->whereTime('register_date', 'week')
            ->count();
        //待跟进
        $this->data['stay_follow_up'] = (new StudentModel())
            ->where($where)
            ->where(['next_follow_up_timestamp' => ['egt', time()]])
            ->whereTime('register_date', 'week')
            ->count();
        //待上门
        $this->data['stay_door'] = (new StudentModel())
            ->where($where)
            ->where(['follow_up' => 36, 'visiting_timestamp' => ['egt', time()]])
            ->whereTime('register_date', 'week')
            ->count();
        //待试听
        $stay_audition_arr = [];
        $already_audition_arr = [];
        $stay_audition = [];
        $already_audition = [];
        if (5 === (int) xcxapi_get_current_wtype()) {
            $stay_audition_arr['user_id'] = xcxapi_get_current_user_id();
        } else {
            $stay_audition['campus_id'] = ['in', xcxapi_get_current_campus_id()];
        }
        $stay_audition['report_status'] = 3;
        $stay_audition['status'] = 0;
        $already_audition['report_status'] = 3;
        $already_audition['status'] = 1;
        $stay_audition_student_ids = (new StudentSubjectInfosClassModel())->where($stay_audition)->column('student_id');
        $stay_audition_arr['id'] = ['in', implode(',', array_values(array_unique($stay_audition_student_ids)))];
        $this->data['stay_audition'] = (new StudentModel())
            ->where($stay_audition_arr)
            ->whereTime('register_date', 'week')
            ->count();
        //已试听
        $already_audition_student_ids = (new StudentSubjectInfosClassModel())->where($already_audition)->column('student_id');
        $already_audition_arr['id'] = ['in', implode(',', array_values(array_unique($already_audition_student_ids)))];
        $this->data['already_audition'] = (new StudentModel())
            ->where($already_audition_arr)
            ->whereTime('register_date', 'week')
            ->count();

        //过期未跟进
        $this->data['overdue_but_not_followed_up'] = (new StudentModel())
            ->where($where)
            ->where(['next_follow_up_timestamp' => ['lt', time()]])
            ->whereTime('register_date', 'week')
            ->count();
    }

    protected function zonglan()
    {
        $where = [];
        if (5 === (int) xcxapi_get_current_wtype()) {
            $where['user_id'] = xcxapi_get_current_user_id();
        } else {
            $where['campus_id'] = ['in', xcxapi_get_current_campus_id()];
        }
        //总客户数
        $this->data['total_customer_number'] = (new StudentModel())
            ->where($where)
            ->count();
        //已分配客户
        $this->data['already_assigned_customers'] = (new StudentModel())
            ->where(['user_id' => ['neq', xcxapi_get_current_user_id()], 'course_consultant_type' => 1, 'status' => 1])
            ->count();
        //公海待分配
        $this->data['high_seas_to_be_distributed'] = (new StudentModel())
            ->where(['user_id' => 0, 'course_consultant_type' => 2, 'status' => 1])
            ->count();
        //已上门
        $this->data['already_arrived'] = (new StudentModel())
            ->where($where)
            ->where(['follow_up' => 103])
            ->count();
        //试听
        $audition_arr = [];
        $audition = [];
        if (5 === (int) xcxapi_get_current_wtype()) {
            $audition['user_id'] = xcxapi_get_current_user_id();
        } else {
            $audition_arr['campus_id'] = ['in', xcxapi_get_current_campus_id()];
        }
        $audition_arr['student_type'] = 3;
        $student_ids = (new StudentSubjectClassModel())->where($audition_arr)->column('student_id');
        $audition['id'] = ['in', implode(',', array_values(array_unique($student_ids)))];
        $this->data['audition'] = (new StudentModel())
            ->where($audition)
            ->count();

        //跟进记录
        $this->data['follow_up_record'] = (new FollowUpLogModel())
            ->where($where)
            ->count();
        //过期未跟进
        $this->data['overdue_but_not_followed_up'] = (new StudentModel())
            ->where($where)
            ->where(['next_follow_up_timestamp' => ['lt', time()]])
            ->count();
    }
}
